package com.party.admin.biz.warn;

import com.party.common.constant.Constant;
import com.party.core.model.system.SysConfig;
import com.party.core.service.system.ISysConfigService;
import com.party.notify.notifyPush.servce.INotifySendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 预警模板
 * Created by wei.li
 *
 * @date 2017/8/4 0004
 * @time 9:47
 */
@Service
public abstract class WarnTemplate {

    @Autowired
    private INotifySendService notifySendService;

    @Autowired
    private ISysConfigService sysConfigService;

    private static final String SMS_WARN_NUM = "smsWarnNum";

    private static final String SMS_CODE_SWITCH = "smsCodeSwitch";
    /**
     * 预警
     */
    public void warn(){
        Integer resourceNum = this.getNum();
        SysConfig sysConfig = sysConfigService.findByCode(SMS_WARN_NUM);
        if (null == sysConfig){
            return;
        }

        Integer targetNum = Integer.valueOf(sysConfig.getValue());
        List<String> receiverList = this.setReceiver();
        String business = this.getBusiness();
        if (resourceNum >=  targetNum){
            for (String receiver : receiverList){

                // 发送短信
                notifySendService.sendWarn(receiver, business );

                //关闭验证码开关
                SysConfig smsCode = sysConfigService.findByCode(SMS_CODE_SWITCH);
                if (null != smsCode && Constant.STR_TRUE.equals(smsCode.getValue())){
                    smsCode.setValue("false");
                    sysConfigService.update(smsCode);
                }
            }
        }
    }

    /**
     * 获取预警阙值
     *
     * @return 阙值
     */
    public abstract Integer getNum();

    /**
     * 设置接受者
     *
     * @return 接受者列表
     */
    public abstract List<String> setReceiver();

    /**
     * 业务名
     *
     * @return 业务名称
     */
    public abstract String getBusiness();

}
